<?php
namespace YunCangChu\Controller;
use Guzzle\Plugin\Cookie\Cookie;
use Think\Controller;
class TimeController extends PublicController {
    public function index()
    {
        //记录这个用户进入了哪个模块
        Cookie('ji',null);
        Cookie('ji',1);
        $url = C('api_url')['仓储类别'];
        $data['FuncName'] = 'GET_CATEGORY_WH';
        $data['LoginID'] = $this->LoginID;
        $app_data = baseMD5($data);
        //返回的参数

        $return_arr = (cUrl($url,$header=null, $app_data));
        //dump($return_arr);die;
        //dump($return_arr['ReturnData']['rows']);die;
        //自动定位
        $baiDuUrl = 'http://api.map.baidu.com/location/ip';
        $ip =  getRealIp();
        if($ip == '127.0.0.1')
        {
            $ip = '';
        }
        $baiDuDate = array('ip'=>$ip,'ak'=>'Nvj0t2xBjkE0GrXOQ1kjKwIAlZ6gz5jf');
        $baiDuReturnJson = cUrl($baiDuUrl,$header=null, $baiDuDate);
        if($baiDuReturnJson['state'] == 0)
        {
            $this->assign('Current_City',$baiDuReturnJson['content']['address_detail']['city']);
        }
        else
        {
            $this->assign('Current_City','');
        }

        $this->assign('type',$return_arr['ReturnData']['rows']);
        $this->display();
    }
    //搜索详情页 调用百度地图
    public function search()
    {
        $url = C('api_url')['获取计时租赁仓列表'];
        //数据
        $data = I('get.');
        $ci = I('get.ci');
        if($ci == 2)
        {
            $data = Cookie('search_where');
        }
        $data['FuncName'] = 'GET_WH_LIST';
        $data['LoginID'] = $this->LoginID;
        Cookie('search_where',$data);
        $app_data = baseMD5($data);
        //dump($app_data);die;
        //返回的参数
        $return_arr = (cUrl($url,$header=null, $app_data));
        //dump($return_arr);die;
        //dump($return_arr['ReturnData']['rows']);die;
        $this->assign('data',$return_arr['ReturnData']['rows']);
        $this->display();
    }
    //列表页
    public function lists()
    {

        $val = I('get.val');
        $url = C('api_url')['获取计时租赁仓列表'];
        $data = Cookie('search_where');
        $data['PageIndex'] = 1;
        if($val)
        {
            $data['Sort'] = $val;
        }
        else
        {
            $data['Sort'] = 'D';
        }
        $app_data = baseMD5($data);
        $return_arr = (cUrl($url,$header=null, $app_data));
        //dump($return_arr);die;
        $this->assign('data',$return_arr['ReturnData']['rows']);
        //dump($return_arr['ReturnData']['rows']);die;
        $this->assign('type',$data['Sort']);



        require_once "jssdk-php/jssdk.php";
        $jssdk = new \JSSDK(C('appId'),C('appSelect'));
        $signPackage = $jssdk->GetSignPackage();
        $this->assign('signPackage',$signPackage);
        $this->display();
    }
    //分页
    public function fenye()
    {
        $url = C('api_url')['获取计时租赁仓列表'];
        $data = Cookie('search_where');
        //页数
        $data['PageIndex'] = I('post.page');
        //类别
        $data['Sort'] = I('post.id');
        $app_data = baseMD5($data);
        $return_arr = (cUrl($url,$header=null, $app_data));
        $data = $return_arr['ReturnData']['rows'];
        if(count($data)<1)
        {
            $this->ajaxReturn(array('code'=>'-1'));
        }
        else
        {
            $str = '';
            foreach($data as $key =>$val)
            {
                //图片
            $picture = $val['Photo'];
            if($picture == '')
            {
                $picture  = 'https://ss0.baidu.com/73x1bjeh1BF3odCf/it/u=143002432,1168190689&fm=85&s=18A27532AD89C0CA5AEDF1C20000E0B1';
            }
                //收藏
                $shoucang = $val['StoreFav'];
                if($shoucang == 'TRUE')
                {
                    $shoucang = "class='btnAfter'";
                }
                else
                {
                    $shoucang = '';
                }
                $str.='<li class="mui-table-view-cell mui-media">
            <a href="javascript:;" class="wspin">
                <img onclick="location.href=\''.U('detail',array('id'=>$val['StoreID'])).'\'" class="mui-media-object mui-pull-left mui-sc-img" src="'.$picture.'">
                <div class="mui-media-body mui-sc-list">
                         <p onclick="location.href=\''.U('detail',array('id'=>$val['StoreID'])).'\'" class="mui-sc-p">'.$val['Name'].'</p>
                    <p onclick="location.href=\''.U('detail',array('id'=>$val['StoreID'])).'\'"><lable>联系人:</lable><i>'.$val['Contact'].'</i></p>
                    <p onclick="location.href=\''.U('detail',array('id'=>$val['StoreID'])).'\'"><lable>联系电话:</lable><i>'.$val['Tel'].'</i></p>
                    <button onclick="shoucang(this)" _id="'.$val['StoreID'].'" type="button" class="mui-btn mui-sc-icon"><i '.$shoucang.'></i> 收藏</button>
                   <button onclick="getLocation(\''.$val['Name'].'\','.$val['Lng'].','.$val['Lat'].')"  type="button" class="mui-btn mui-dh-icon"><i></i>导航</button>
                </div>
            </a>
        </li>';
            }
            $this->ajaxReturn(array('code'=>'1','str'=>$str));
        }

    }
    //设置收藏
    public function setSc()
    {
        $id = I('post.id');
        if($id == '')
        {
            return false;
        }
        $url = C('api_url')['收藏仓库'];
        $data['FuncName'] = 'WH_FAV_ADD';
        $data['LoginID'] = $this->LoginID;
        $data['StoreID'] = $id;
        $app_data = baseMD5($data);
        $return_arr = (cUrl($url,$header=null, $app_data));
        if($return_arr['ReturnCode'] == 0)
        {
            $this->ajaxReturn(array('code'=>'0'));
        }
        else
        {
            $this->ajaxReturn(array('code'=>'-1'));
        }
    }
    //取消收藏
    public function qetSc()
    {
        $id = I('post.id');
        if($id == '')
        {
            return false;
        }
        $url = C('api_url')['取消收藏仓库'];
        $data['FuncName'] = 'WH_FAV_DE';
        $data['LoginID'] = $this->LoginID;
        $data['StoreID'] = $id;
        $app_data = baseMD5($data);
        $return_arr = (cUrl($url,$header=null, $app_data));
        if($return_arr['ReturnCode'] == 0)
        {
            if($return_arr['ReturnCode'] == 0)
            {
                $this->ajaxReturn(array('code'=>'0'));
            }
            else
            {
                $this->ajaxReturn(array('code'=>'-1'));
            }
        }
    }
    //仓库详情
    public function detail()
    {
        $jingdu = Cookie('search_where')['LocLng'];
        $weidu =  Cookie('search_where')['LocLat'];
        $url = C('api_url')['即使仓库详情'];
        $data['FuncName'] = 'GET_WH_DETAIL';
        $data['LoginID'] =$this->LoginID;
        $data['StoreID'] =I('get.id');
        $data['Lng'] =$jingdu;
        $data['Lat'] =$weidu;
        $app_data = baseMD5($data);
        $return_arr = (cUrl($url,$header=null, $app_data));
        $this->assign('cangku',$data['StoreID']);
        $this->assign('data',$return_arr['ReturnData']);
        $this->display();
    }
    //仓库预约
    public function yuyue()
    {
        $url = C('api_url')['获取计时租赁存放物品类别'];
        $data['FuncName'] = 'GET_CATEGORY_GOODS';
        $data['LoginID'] =$this->LoginID;
        $app_data = baseMD5($data);
        $return_arr = (cUrl($url,$header=null, $app_data));
        $time = I('get.time');
        $this->assign('time',$time);
        $this->assign('data',$return_arr['ReturnData']['rows']);

        $url = C('api_url')['仓储类别'];
        $data = array();
        $data['FuncName'] = 'GET_CATEGORY_WH';
        $data['LoginID'] =$this->LoginID;
        $app_data = baseMD5($data);
        $return_arr = (cUrl($url,$header=null, $app_data));
        $this->assign('type',$return_arr['ReturnData']['rows']);
        //dump($return_arr['ReturnData']['rows']);die;

        //时间判断 确认调用哪种时间插件
        $times=substr(Cookie('search_where')['StartDate'],-4);
        if($times == '0:00')
        {
            //不调用时分秒
            $time = 1;
        }
        else
        {
            $time = 2;
            //调用时分秒
        }
        $this->assign('times',$time);

        //获取省市县
        $url = C('api_url')['全国行政三级联动数据'];
        $data=array();
        $data['FuncName'] = 'GET_REGION';
        $data['LoginID'] = $this->LoginID;
        $app_data = baseMD5($data);
        //返回的参数
        $return_arr = (cUrl($url,$header=null, $app_data));
        S('city',$return_arr,86400);
        $this->assign('citys',$return_arr);
        //dump($return_arr);die;


        $this->display();
    }
    //地图开启导航
    public function daohang()
    {
        $id = I('get.id');
        $jing = I('get.jing');
        $wei = I('get.wei');
        //获取目标经纬度城市名称
       // $api_url = 'http://api.map.baidu.com/geocoder/v2/?callback=renderReverse&location='.$wei.','.$jing.'&output=json&pois=1&ak=Nvj0t2xBjkE0GrXOQ1kjKwIAlZ6gz5jf';
        //$return = file_get_contents($api_url);
        //获取当前经纬度城市名称
        //echo $return;
        //$_SERVER['SERVER_NAME']
        header("Location:/jssdk-php/sample.php");
    }
    //确认预约
    public function queren()
    {
        $url = C('api_url')['计时租赁订单保存'];
        $data = I('get.');
        $data['FuncName'] = 'ORDER_SAVE';
        $data['LoginID'] =$this->LoginID;
        $data['SelfPick'] = 'true';
        unset($data['g']);
        //print_r($data);die;
        $app_data = baseMD5($data);
        //dump($app_data);die;
        $return_arr = cUrl($url,$header=null, $app_data);
        if($return_arr['ReturnCode'] == 0)
        {
            $this->ajaxReturn(array('code'=>'0','url'=>$return_arr['ReturnData']));
        }
        else
        {
            $this->ajaxReturn(array('code'=>'-1','msg'=>$return_arr['ReturnText']));
        }
    }
    //获取计时租赁订单待支付明细
    public function mingxi()
    {
        //201803100001
        $url = C('api_url')['返回订单详情'];
        $orderid = I('id');
        $data['FuncName'] = 'GET_ORDER_DETAIL_TOPAID';
        $data['LoginID'] = $this->LoginID;
        $data['OrderSN'] = $orderid;
        $app_data = baseMD5($data);
        //返回的参数   计算方法   单价*面积*时间
        //缺少字段 Unit：租期单位：Day：按天Hour：按小时
        //print_r($app_data);die;
        $return_arr = (cUrl($url,$header=null, $app_data));
        //print_r($return_arr);die;
        $this->assign('data',$return_arr['ReturnData']);
        $this->display();
    }
    //搜索获取城市列表
/*Array
(
[ReturnCode] => 0
[ReturnText] =>
[ReturnData] => Array
(
[total] => 1
[rows] => Array
(
[0] => Array
(
[id] => 110000
[name] => 北京市
)
)
)

)*/
    public function get_city_list()
    {
        $val = I('post.val');
        if($val == '')
        {
          //查询不到数据
          $this->ajaxReturn(array('code'=>'-1'));
        }
        $url = C('api_url')['城市搜索'];
        $data['FuncName'] = 'CITY_SEARCH';
        $data['LoginID'] = $this->LoginID;
        $data['Keyword'] = $val;
        $data['OrderSN'] = $val;
        $app_data = baseMD5($data);
        $return_arr = (cUrl($url,$header=null, $app_data));
        $this->ajaxReturn(array('code'=>1,'html'=>$return_arr['ReturnData']));
    }
    public function choseCity()
    {
        $baiDuUrl = 'http://api.map.baidu.com/location/ip';
        $ip =  getRealIp();
        if($ip == '127.0.0.1')
        {
            $ip = '';
        }
        $baiDuDate = array('ip'=>$ip,'ak'=>'Nvj0t2xBjkE0GrXOQ1kjKwIAlZ6gz5jf');
        $baiDuReturnJson = cUrl($baiDuUrl,$header=null, $baiDuDate);
        if($baiDuReturnJson['state'] == 0)
        {
            $this->assign('Current_City',$baiDuReturnJson['content']['address_detail']['city']);
        }
        else
        {
            $this->assign('Current_City','');
        }
        //获取城市列表
        $url = C('api_url')['获取城市列表'];
        $data = array();
        $data['FuncName'] = 'GET_CITY_LIST';
        $data['LoginID'] = $this->LoginID;
        $app_data = baseMD5($data);
        //返回的参数
        $return_arr = (cUrl($url,$header=null, $app_data));
        $this->assign('cityList',$return_arr['ReturnData']);
        //print_r($return_arr['ReturnData']);die;
        $this->display();
    }
    //选择优惠卷
    public function selectTheCoupon()
    {
        $url = C('api_url')['返回订单详情'];
        //订单id
        $orderid =  I('get.id');
        $data['FuncName'] = 'GET_ORDER_DETAIL_TOPAID';
        $data['LoginID'] = $this->LoginID;
        $data['OrderSN'] = $orderid;
        $app_data = baseMD5($data);
        //返回的参数   计算方法   单价*面积*时间
        //缺少字段 Unit：租期单位：Day：按天Hour：按小时
        //print_r($app_data);die;
        $return_arr = (cUrl($url,$header=null, $app_data));
        //print_r($return_arr);die;
        $this->assign('data',$return_arr['ReturnData']['Coup']);
        $this->display();
    }

}